package com.springmvc.shangguigu.dao;

import com.springmvc.shangguigu.entities.Department;
import com.springmvc.shangguigu.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * EmployeeDao--SpringMVC-test
 *
 * @author: Jovi
 * @createTime: 2018-09-10 15:34
 **/
@Repository
public class EmployeeDao {

    private static Map<Integer,Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    static{

        employees = new HashMap<>();
        employees.put(1001,new Employee(1001,"E-AA","aa@",1,new Department(101,"D-AA")));
        employees.put(1002,new Employee(1002,"E-BB","aa@",1,new Department(102,"D-BB")));
        employees.put(1003,new Employee(1003,"E-CC","aa@",0,new Department(103,"D-CC")));
        employees.put(1004,new Employee(1004,"E-DD","aa@",0,new Department(104,"D-DD")));
        employees.put(1005,new Employee(1005,"E-EE","aa@",1,new Department(105,"D-EE")));
    }

    private static Integer intiId = 1006;

    public void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(intiId++);
        }

        employee.setDepartment(departmentDao.getDepartMent(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }

    public Collection<Employee> getAll() {
        return employees.values();
    }

    public Employee get(Integer id) {
        return employees.get(id);
    }

    public void delete(Integer id) {
        employees.remove(id);
    }

}
